package org.abratuhi.kvngroupware.client.widget;

import java.awt.event.ActionEvent;
import java.util.Properties;

import javax.swing.AbstractAction;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import org.abratuhi.kvngroupware.brainstorm.manager.BrainstormManager;
import org.abratuhi.kvngroupware.brainstorm.widget.BrainstormlistWidget;
import org.abratuhi.kvngroupware.chat.manager.ChatroomManager;
import org.abratuhi.kvngroupware.chat.widget.ChatroomlistWidget;
import org.abratuhi.kvngroupware.dalgen.DalgenManager;
import org.abratuhi.kvngroupware.dalgen.DalgenWidget;
import org.abratuhi.kvngroupware.jokemanager.manager.JokeManager;
import org.abratuhi.kvngroupware.jokemanager.widget.GuiJokeManager;
import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.login.widget.GuiLogin;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.registration.manager.RegistrationManager;
import org.abratuhi.kvngroupware.registration.widget.GuiRegistration;
import org.abratuhi.kvngroupware.scenario.manager.ScenarioManager;
import org.abratuhi.kvngroupware.scenario.widget.ScenariolistWidget;
import org.abratuhi.kvngroupware.user.manager.UserManager;
import org.abratuhi.kvngroupware.user.widget.UserlistWidget;
import org.abratuhi.kvngroupware.widget.WidgetManager;
import org.jdom.Element;

@SuppressWarnings("serial")
public class GuiRootMenubar extends JMenuBar{
	
	MessageSender sender;
	Properties locale;
	LoginManager loginmanager;
	RegistrationManager regmanager;
	UserManager usermanager;
	ChatroomManager chatroommanager;
	BrainstormManager brainstormmanager;
	ScenarioManager scenariomanager;
	JokeManager jokemanager;
	DalgenManager dalgenmanager;
	WidgetManager widgetmanager;
	
	JMenu mAction;
	JMenuItem miRegister;
	JMenuItem miLogin;
	JMenuItem miLogout;
	JMenuItem miExit;
	
	JMenu mView;
	JCheckBoxMenuItem miUsers;
	JCheckBoxMenuItem miBrainstorms;
	JCheckBoxMenuItem miChatrooms;
	JCheckBoxMenuItem miScenarios;
	JCheckBoxMenuItem miJokemanagers;
	JCheckBoxMenuItem miDalgen;
	
	JMenu mHelp;
	JMenuItem miHelp;
	JMenuItem miAbout;
	
	public GuiRootMenubar(MessageSender sender, Properties locale,
			LoginManager loginmanager, RegistrationManager regmanager, UserManager usermanager, ChatroomManager chatroommanager, 
			BrainstormManager brainstormmanager, ScenarioManager scenariomanager, JokeManager jokemanager, DalgenManager dalgenmanager, 
			WidgetManager widgetmanager){
		super();
		
		this.sender = sender;
		this.locale = locale;
		this.loginmanager = loginmanager;
		this.regmanager = regmanager;
		this.usermanager = usermanager;
		this.chatroommanager = chatroommanager;
		this.brainstormmanager = brainstormmanager;
		this.scenariomanager = scenariomanager;
		this.jokemanager = jokemanager;
		this.dalgenmanager = dalgenmanager;
		this.widgetmanager = widgetmanager;
		
		mAction = new JMenu(locale.getProperty("main.menu.action"));
		miRegister = new JMenuItem(locale.getProperty("main.menuitem.register"));
		miLogin = new JMenuItem(locale.getProperty("main.menuitem.login"));
		miLogout = new JMenuItem(locale.getProperty("main.menuitem.logout"));
		miExit = new JMenuItem(locale.getProperty("main.menuitem.exit"));
		
		mView = new JMenu(locale.getProperty("main.menu.view"));
		miUsers = new JCheckBoxMenuItem(locale.getProperty("main.menuitem.users"));
		miBrainstorms = new JCheckBoxMenuItem(locale.getProperty("main.menuitem.brainstorms"));
		miChatrooms = new JCheckBoxMenuItem(locale.getProperty("main.menuitem.chatrooms"));
		miScenarios = new JCheckBoxMenuItem(locale.getProperty("main.menuitem.scenarios"));
		miJokemanagers = new JCheckBoxMenuItem(locale.getProperty("main.menuitem.jokemanagers"));
		miDalgen = new JCheckBoxMenuItem(locale.getProperty("main.menuitem.dalgen"));
		
		mHelp = new JMenu(locale.getProperty("main.menu.help"));
		miHelp = new JMenuItem(locale.getProperty("main.menuitem.help"));
		miAbout = new JMenuItem(locale.getProperty("main.menuitem.about"));
		
		
		mAction.add(miRegister);
		mAction.add(miLogin);
		mAction.add(miLogout);
		mAction.addSeparator();
		mAction.add(miExit);
		
		mView.add(miUsers);
		mView.add(miChatrooms);
		mView.add(miBrainstorms);
		mView.add(miScenarios);
		mView.add(miJokemanagers);
		mView.add(miDalgen);
		
		mHelp.add(miHelp);
		mHelp.add(miAbout);
		
		
		miRegister.setAction(new RegisterAction(locale.getProperty("main.menuitem.register")));
		miLogin.setAction(new LoginAction(locale.getProperty("main.menuitem.login")));
		miLogout.setAction(new LogoutAction(locale.getProperty("main.menuitem.logout")));
		miExit.setAction(new ExitAction(locale.getProperty("main.menuitem.exit")));
		
		miUsers.setAction(new UViewAction(locale.getProperty("main.menuitem.users")));
		miBrainstorms.setAction(new BViewAction(locale.getProperty("main.menuitem.brainstorms")));
		miChatrooms.setAction(new CViewAction(locale.getProperty("main.menuitem.chatrooms")));
		miScenarios.setAction(new SViewAction(locale.getProperty("main.menuitem.scenarios")));
		miJokemanagers.setAction(new JViewAction(locale.getProperty("main.menuitem.jokemanager")));
		miDalgen.setAction(new DViewAction(locale.getProperty("main.menuitem.dalgen")));		
		
		add(mAction);
		add(mView);
		add(mHelp);
		
		setVisible(true);
		revalidate();
	}
	
	class RegisterAction extends AbstractAction{
		public RegisterAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			sender.sendMessage(sender.createMessage("type,action", "team,get", "", new Element[0]));
			GuiRegistration guireg = new GuiRegistration(sender, locale, regmanager);
			widgetmanager.put("register", guireg);
			guireg.createWindow();
		}
	}
	class LoginAction extends AbstractAction{
		public LoginAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			GuiLogin guilogin = new GuiLogin(sender, loginmanager, locale);
			widgetmanager.put("login", guilogin);
			guilogin.createWindow();
		}
	}
	class LogoutAction extends AbstractAction{
		public LogoutAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			sender.sendMessage(
					sender.createMessage(
							"type,action",
							"user,logout",
							"",
							new Element[]{loginmanager.getUser().toElement()}));
		}
	}
	class ExitAction extends AbstractAction{
		public ExitAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			sender.sendMessage(
					sender.createMessage(
							"type,action",
							"user,logout",
							"",
							new Element[]{loginmanager.getUser().toElement()}));
			System.exit(0);
		}
	}
	
	class UViewAction extends AbstractAction{
		public UViewAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e){
			JCheckBoxMenuItem src = (JCheckBoxMenuItem) e.getSource();
			if(src.isSelected()) {
				widgetmanager.put("userlist", new UserlistWidget("List: User", sender, locale, usermanager));
				widgetmanager.get("userlist").createWindow();
			}
			else{
				widgetmanager.get("userlist").deleteWindow();
			}
		}
	}
	class CViewAction extends AbstractAction{
		public CViewAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e){
			JCheckBoxMenuItem src = (JCheckBoxMenuItem) e.getSource();
			if(src.isSelected()) {
				widgetmanager.put("chatroomlist", new ChatroomlistWidget("List: Chatrooms", sender, locale, loginmanager, chatroommanager));
				widgetmanager.get("chatroomlist").createWindow();
			}
			else{
				widgetmanager.get("chatroomlist").deleteWindow();
			}
		}
	}
	class BViewAction extends AbstractAction{
		public BViewAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e){
			JCheckBoxMenuItem src = (JCheckBoxMenuItem) e.getSource();
			if(src.isSelected()) {
				widgetmanager.put("brainstormlist", new BrainstormlistWidget("List: Brainstorms", sender, locale, loginmanager, brainstormmanager));
				widgetmanager.get("brainstormlist").createWindow();
			}
			else{
				widgetmanager.get("brainstormlist").deleteWindow();
			}
		}
	}
	class SViewAction extends AbstractAction{
		public SViewAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e){
			JCheckBoxMenuItem src = (JCheckBoxMenuItem) e.getSource();
			if(src.isSelected()) {
				widgetmanager.put("scenariolist", new ScenariolistWidget("List: Scenarios", sender, locale, scenariomanager, loginmanager));
				widgetmanager.get("scenariolist").createWindow();
			}
			else{
				widgetmanager.get("scenariolist").deleteWindow();
			}
		}
	}
	class JViewAction extends AbstractAction{
		public JViewAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e){
			JCheckBoxMenuItem src = (JCheckBoxMenuItem) e.getSource();
			if(src.isSelected()) {
				widgetmanager.put("jokemanagerlist", new GuiJokeManager("jokemanager", "Jokemanager", sender, locale, loginmanager, jokemanager));
				widgetmanager.get("jokemanagerlist").createWindow();
			}
			else{
				widgetmanager.get("jokemanagerlist").deleteWindow();
			}
		}
	}
	class DViewAction extends AbstractAction{
		public DViewAction(String text){
			super(text, null);
		}
		@Override
		public void actionPerformed(ActionEvent e){
			JCheckBoxMenuItem src = (JCheckBoxMenuItem) e.getSource();
			if(src.isSelected()) {
				widgetmanager.put("dalgen", new DalgenWidget("List: Dalgen", sender, locale, dalgenmanager));
				widgetmanager.get("dalgen").createWindow();
			}
			else{
				widgetmanager.get("dalgen").deleteWindow();
			}
		}
	}
}
